package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.dao.EmpPostMapper;
import com.yjt.admin.system.entity.EmpPost;
import com.yjt.admin.system.entity.Emply;
import com.yjt.admin.system.service.EmpPostService;
import com.yjt.admin.system.service.EmplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工岗位表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-11
 */
@Service
public class EmpPostServiceImpl extends ServiceImpl<EmpPostMapper, EmpPost> implements EmpPostService {

    @Autowired
    private EmplyService emplyService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEmpPost(String postId, Integer postType, List<String> emplyIdList) {
        //存放员工岗位关联信息
        List<EmpPost> empPostPoList = new ArrayList<>();

        //存放员工关联信息
        List<Emply> emplyPoList = new ArrayList<>();

        //构造参数
        emplyIdList.stream().forEach(emplyId -> {
            empPostPoList.add(EmpPost.builder().emplyId(emplyId).postId(postId).build());
            this.setPostIdWithEmply(postId, postType, emplyPoList, emplyId);
        });

        //保存员工岗位关联信息
        this.saveBatch(empPostPoList);

        //修改员工信息
        emplyService.updateBatchById(emplyPoList);
    }

    /**
     * 设置员工中的岗位id
     *
     * @param postId
     * @param postType
     * @param emplyList
     * @param emplyId
     */
    private void setPostIdWithEmply(String postId, Integer postType, List<Emply> emplyList, String emplyId) {
        Emply emplyPo = Emply.builder().emplyId(emplyId).build();
        //判断一下是虚拟岗位还是实际岗位
        if (SysCons.POST_TYPE[1] == postType) {
            //实际岗位
            emplyPo.setPostId(postId);
        } else {
            //虚拟岗位
            emplyPo.setImagePostId(postId);
        }
        emplyList.add(emplyPo);
    }
}
